package com.example.demo;

import java.util.Arrays;

class Solution {
    public static void main(String[] args) {
        int[] nums1 = {4,9,5};
        int[] nums2 = {9,4,9,8,4};
        int[] intersection = intersection(nums1, nums2);
        System.out.println(Arrays.toString(intersection));
    }
    public static int[] intersection(int[] nums1, int[] nums2) {
        //创建两个新数组
        int[] array1 = new int[10];
        int[] array2 = new int[10];

        //遍历nums1放到array1
        for(int i = 0;i < nums1.length;i++){
            array1[nums1[i]]++;
        }
        //遍历nums2放到array2
        for(int i = 0;i < nums2.length;i++){
            array2[nums2[i]]++;
        }

        //将array1和array2相减
        for(int i = 0;i < array1.length;i++){
            array1[i] = Math.abs(array1[i] - array2[i]);
        }

        //新数组的长度
        int length = 0;
        for(int i = 0;i < array1.length;i++){
            length = length + array1[i];
        }

        //新数组
        int[] newArray = new int[length];

        //放数据
        int idx = 0;
        for(int i = 0;i < array1.length;i++){
            for(int j = array1[i];j > 0;j--){
                newArray[idx] = i;
                idx++;
            }
        }
        return newArray;
    }
}
